<template>
  <div class="rule">
    <div v-if="panes?.length">
      <el-tabs v-model="active" tab-position="top" @tab-click="tabClick">
        <el-tab-pane
          v-for="item in panes"
          :key="item.key"
          :label="item.label"
          :name="item.key"
        >
        </el-tab-pane>
        <el-row :gutter="20">
          <el-col :span="active == '5' ? 24 : 12">
            <component :is="itemPage" :active="active"></component>
          </el-col>
        </el-row>
      </el-tabs>
    </div>
    <div v-else>请先设置预警模型</div>
  </div>
</template>

<script>
import indexForm from './indexForm'
import indexTable from './indexTable'
import { findListByPage, findWarningModelRuleList } from '@/api/rule'
export default {
  name: 'rule',
  components: {
    indexForm,
    indexTable
  },
  data() {
    return {
      active: '1',
      panes: [
        // { label: '家暴人员预警规则', key: '1' },
        // { label: '企业安全生产预警规则', key: '2' },
        // { label: '群租房预警规则', key: '3' },
        // { label: '企业欠薪预警规则', key: '4' },
        // { label: '高发预警预警规则', key: '5' }
      ],
      itemPage: '',
      warningList: [],
      data: {}
    }
  },
  created() {
    this.user = this.$store.state.user.user
    this.poltype = this.user.poltype
    this.findWarningModelRuleList()
    // this.findListByPage() // 废弃

    if (this.active == '5') {
      this.itemPage = indexTable
    } else {
      this.itemPage = indexForm
    }
  },
  methods: {
    tabClick(val) {
      this.active = val.name
      localStorage.modelType = val.name
      if (this.active == '5') {
        this.itemPage = indexTable
      } else {
        this.itemPage = indexForm
      }

      this.warningList.map((i) => {
        if (i.modelType == this.active) {
          this.data = i
        }
      })
    },
    findListByPage() {
      let params = {
        pageNum: 1,
        pageSize: 10,
        poltype: this.poltype
      }
      findListByPage(params).then((res) => {
        if (!res.success) return
        let list = res.data.list
        this.warningList = list
        this.data = list[0]
      })
    },
    findWarningModelRuleList() {
      findWarningModelRuleList().then((res) => {
        if (!res.success) return
        this.panes = []
        res.data.map((i) => {
          this.panes.push({ label: i.ruleName, key: i.modelType + '' })
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>